package com.yuan.goods;

import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.Calendar;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;

import com.yuan.common.domain.BaseDomain;
import com.yuan.merchant.Merchant;

/**
 * 
 * 固定格式的商品文件，从网上抓取后，需转成此类型的文件
 * 
 * @author Beni
 * 
 */
public class Goods extends BaseDomain {

	private static final long serialVersionUID = -8959403394359113917L;

	private String title;
	private String simpleTitle; // 显示的title
	private String siteurl;
	private String city;
	private String img;
	private Double starttime;
	private Double endtime;
	private String value; // 价值，即原价
	private String price; // 折后价
	private String discount; // 折扣，一般都为0
	private String description; // 描述
	private String remark; // 备注
	private Double curtime; // 用于页面的展示
	private Double diff; // 用于页面展示
	private Integer merchantId; // 商家id
	private Integer priority; // 显示时的优先级

	private Double diffDomain; // 用于持久化
	private Double currentDomain; // 用于持久化

	private Merchant merchant;

	private Integer auto; // 是否为自动抓取

	private String tags; // 分类标签,以,号分隔

	private Timestamp expired; // 过期时间

	@XmlElement(name = "title")
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@XmlElement(name = "siteurl")
	public String getSiteurl() {
		return siteurl;
	}

	public void setSiteurl(String siteurl) {
		this.siteurl = siteurl;
	}

	@XmlElement(name = "city")
	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	@XmlElement(name = "img")
	public String getImg() {
		return img;
	}

	public void setImg(String img) {
		this.img = img;
	}

	public Double getStarttime() {
		return starttime;
	}

	public void setStarttime(Double starttime) {
		this.starttime = starttime;
	}

	public Double getEndtime() {
		return endtime;
	}

	public void setEndtime(Double endtime) {
		this.endtime = endtime;
	}

	@XmlElement(name = "value")
	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	@XmlElement(name = "price")
	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	@XmlElement(name = "discount")
	public String getDiscount() {
		return discount;
	}

	public void setDiscount(String discount) {
		this.discount = discount;
	}

	@XmlElement(name = "description")
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@XmlElement(name = "remark")
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Long getCurtime() {
		return Calendar.getInstance().getTimeInMillis();
	}

	public void setCurtime(Double curtime) {
		this.curtime = curtime;
	}

	public Double getDiff() {
		return diff;
	}

	public void setDiff(Double diff) {
		this.diff = diff;
	}

	@XmlElement(name = "priority")
	public Integer getPriority() {
		return priority;
	}

	public void setPriority(Integer priority) {
		this.priority = priority;
	}

	@XmlElement(name = "merchantId")
	public Integer getMerchantId() {
		return merchantId;
	}

	public void setMerchantId(Integer merchantId) {
		this.merchantId = merchantId;
	}

	public Merchant getMerchant() {
		return merchant;
	}

	public void setMerchant(Merchant merchant) {
		this.merchant = merchant;
	}

	public Double getCurrentDomain() {
		return currentDomain;
	}

	public void setCurrentDomain(Double currentDomain) {
		this.currentDomain = currentDomain;
	}

	public Double getDiffDomain() {
		return diffDomain;
	}

	public void setDiffDomain(Double diffDomain) {
		this.diffDomain = diffDomain;
	}

	public String getSimpleTitle() {
		// 控制页面显示
		if (this.title != null && this.title.length() > 40) {
			return this.title.substring(0, 40) + "...";
		}
		return title;
	}

	public void setSimpleTitle(String simpleTitle) {
		this.simpleTitle = simpleTitle;
	}

	@XmlElement(name = "auto")
	public Integer getAuto() {
		return auto;
	}

	public void setAuto(Integer auto) {
		this.auto = auto;
	}

	@XmlElement(name = "tags")
	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	@XmlTransient 
	public Timestamp getExpired() {
		return expired;
	}

	public void setExpired(Timestamp expired) {
		this.expired = expired;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("<style tyoe='text/css'>table{border-collapse:collapse; } td{border:1px solid blue;}</style>");
		try {
			sb.append("<table>");
			Class goodsClazz = Goods.class;

			Method[] methods = goodsClazz.getDeclaredMethods();
			for (Method me : methods) {

				if (me.getName().startsWith("get")) {
					sb.append("<tr>");
					sb.append("<td>");
					sb.append(me.getName().replaceAll("get", ""));
					sb.append("</td>");
					sb.append("<td>");
					sb.append(me.invoke(this));
					sb.append("</td>");
					sb.append("</tr>");
				}
			}

			sb.append("</table>");
		} catch (Exception e) {
			return "";
		}
		return sb.toString();
	}
}
